<template>
  <el-container class="home-container">
    <!--头部区域-->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span class="banner">电商后台管理系统</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse? '64px' :'200px'">
        <!--通过三目运算符控制侧边栏的宽度，iscollapse为true时，压缩宽度为64px，false为200px-->
        <div class="toggle-button" @click="toggle">|||</div>
        <!--侧边栏菜单区-->
        <el-menu background-color="#333744" text-color="#fff" unique-opened="" :collapse="isCollapse"
                 :collapse-transition="false" router
                 default-active="this.$route.path">
          <!--一级菜单-->
          <!--el-menu中添加router属性可以启动路由跳转，将每个子菜单的id值作为访问的url-->
          <el-submenu :index="item.path+''" v-for="item in menuList" :key="item.id">
            <!--一级菜单的模板区域-->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!--二级菜单-->
            <!--在el-menu里面加router，激活router属性，然后点子菜单就在子菜单中的id，注意要拼接斜线字符串-->
            <el-menu-item :index="'/'+i.path" v-for="i in item.children" :key="i.id">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{i.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--右侧主体-->
      <el-main>
        <!--welcome的路由占位符-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>
<script>
  export default {
    data() {
      return {
        // 左侧菜单数据
        menuList: [],
        iconsObj: {
          125: 'el-icon-s-promotion',
          103: 'el-icon-upload',
          101: 'el-icon-loading',
          102: 'el-icon-caret-right',
          145: 'el-icon-s-data',
        },
        isCollapse: false,


      }
    },
    created() {
      this.getMenuList()
    },
    methods: {
      logout() {
        window.sessionStorage.clear()
        this.$router.push('/login')
        this.$message.success('退出成功')
      },
      async getMenuList() {
        let {data: res} = await this.$http.get('menus')
        // console.log(res);
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.menuList = res.data
      },
      toggle() {
        this.isCollapse = !this.isCollapse

      }
    }
  }
</script>
<style lang="less" scoped>
  .home-container {
    height: 100%;
  }

  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    /*让盒子居中对齐*/
    color: #fff;
    font-size: 20px;

    div {
      display: flex;
      align-items: center;

      span {
        margin-left: 15px;
      }
    }
  }

  .el-aside {
    background-color: #333744;

    .el-menu {
      border-right: 0;
    }
  }

  .el-main {
    background-color: #eaedf1;
  }

  .toggle-button {
    background-color: #4A5064;
    color: #fff;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    /*注意letter-spacing调节字母间的距离*/
    cursor: pointer;
  }
</style>